<?php
/**
 * author: Zhang Rui
 * github: https://github.com/zhangrui-git
 * mail: zhangruirui@zhangruirui.com
 * create: 2021/6/27 下午5:34
 */


class Stage
{
    public function __construct()
    {

    }

    public static function Move(array &$map, array $point): int
    {
        $step = 0;
        foreach ($point as [$_x, $_y]) {
            if (isset($map[$_x][$_y])) {
                self::Point($map, $_x, $_y, 3);
                $step++;
            }
        }
        return $step;
    }

    public static function Direction(array &$map, int $x, int $y, array $hinder = []): array
    {
        // 奇数
        if ($x & 1) {
            $d = [
                [$x, $y - 1],
                [$x - 1, $y - 1],
                [$x - 1, $y],
                [$x, $y + 1],
                [$x + 1, $y],
                [$x + 1, $y - 1],
            ];
        }
        // 偶数
        else {
            $d = [
                [$x, $y - 1],
                [$x - 1, $y],
                [$x - 1, $y + 1],
                [$x, $y + 1],
                [$x + 1, $y + 1],
                [$x + 1, $y],
            ];
        }
        foreach ($d as $k => [$_x , $_y]) {
            if (!isset($map[$_x][$_y]) ||  in_array($map[$_x][$_y], $hinder)) {
                $d[$k] = [0, 0];
            }
        }
        return $d;
    }

    public static function Point(array &$map, int $x, int $y, int $v): ?int
    {
        if (isset($map[$x][$y])) {
            return $map[$x][$y] = $v;
        }
        return null;
    }

    public static function MergeMap(array &$map1, array &$map2)
    {
        $rSize = count($map1);
        for ($i = 0; $i < $rSize; $i++) {
            $cSize = count($map1[$i]);
            for ($j = 0; $j < $cSize; $j++) {
                if (isset($map2[$i][$j]) && $map2[$i][$j] > 0) {
                    $map1[$i][$j] = $map2[$i][$j];
                }
            }
        }
    }

    public static function NewMap(int $width, int $height): array
    {
        return [
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
        ];
    }

    public static function Count(int $width, int $height): int
    {
        if ($height & 1) {
            $c = ($height - 1) / 2 * ($width * 2 - 1) + $width - 1;
        } else {
            $c = $height / 2 * ($width * 2 - 1);
        }
        return $c;
    }

    public static function CoordinateToNumber(array &$map, int $x, int $y): ?int
    {
        $n = null;
        if ($x) {
            for ($i = 0; $i < $x; $i++) {
                $n += count($map[$i]);
            }
            $n += $y + 1;
        } else {
            $n = $y + 1;
        }
        return $n;
    }

    public static function NumberToCoordinate(array &$map, int $number): ?array
    {
        $c = null;
        $n = 0;
        foreach ($map as $r => $line) {
            $l = count($line);
            $n += $l;
            if ($n >= $number) {
                $c = [$r, $l - ($n  - $number) - 1];
                break;
            }
        }
        return $c;
    }

    public static function Render(array $map): void
    {
        echo PHP_EOL;
        foreach ($map as $r => $line) {
            if (!($r & 1)) {
                echo ' ';
            }
            foreach ($line as $c => $grid) {
                switch ($grid) {
                    case 0:
                        echo '□';
                        break;
                    case 1:
                        echo '⚐';
                        break;
                    case 2:
                        echo '⚑';
                        break;
                    case 3:
                        echo '●';
                        break;
                    case 9:
                        echo '▲';
                        break;
                }
                echo ' ';
            }
            echo PHP_EOL;
        }
        echo PHP_EOL;
    }
}